package com.sec.etech.file.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.sec.etech.file.dto.FileUploadDTO;
import com.sec.etech.file.model.EtechFileTemp;
import com.sec.etech.file.service.EtechFileTempService;
import com.sec.etech.file.service.EtechWebUploaderService;
import org.openbpm.base.api.aop.annotion.CatchErr;
import org.openbpm.base.api.exception.BusinessMessage;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.rest.ControllerTools;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sys/etechFile/webuploader/")
public class EtechWebUploaderController extends ControllerTools {

    @Resource
    EtechWebUploaderService etechWebUploaderService;
    @Resource
    EtechFileTempService etechFileTempService;

    /**
     * 上传文件到临时文件夹
     *
     * @param fileUploadDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @CatchErr(value = "上传失败")
    public ResultMsg<cn.hutool.json.JSONObject> upload(FileUploadDTO fileUploadDTO) throws Exception {
        return getSuccessResult(etechWebUploaderService.upload(fileUploadDTO), "上传成功");
    }

    /**
     * 分片上传后的文件合并
     *
     * @param fileUploadDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "merge", method = RequestMethod.POST)
    @CatchErr(value = "合并失败")
    public ResultMsg<EtechFileTemp> merge(@RequestBody FileUploadDTO fileUploadDTO) throws Exception {
        EtechFileTemp etechFileTemp = etechWebUploaderService.merge(fileUploadDTO);
        return getSuccessResult(etechFileTemp, "合并成功");
    }

    /**
     * 移动临时文件到正式文件
     *
     * @param fileids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "moveTempFileToFormalFile", method = RequestMethod.POST)
    @CatchErr(value = "移动临时文件到正式文件失败")
    public ResultMsg moveTempFileToFormalFile(String fileids) throws Exception {
        String[] ids = fileids.split(",");
        for (String id : ids) {
            if (StrUtil.isBlank(id)) {
                throw new BusinessMessage("移动临时文件到正式文件失败，文件id不能为空！");
            }
            EtechFileTemp etechFileTemp = etechFileTempService.getById(id);
            etechWebUploaderService.moveTempFileToFormalFile(etechFileTemp);
        }
        return getSuccessResult(true, "移动临时文件到正式文件成功");
    }

    /**
     * 保存表单后
     *
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "afterSaveFile", method = RequestMethod.POST)
    @CatchErr(value = "保存表单后操作文件失败")
    public ResultMsg afterSaveFile(@RequestBody com.alibaba.fastjson.JSONObject data) throws Exception {
        if (!JSONUtil.isNull(data)) {
            etechWebUploaderService.handlerFileAfterSaveForm(data);
        }
        return getSuccessResult(true, "保存表单后操作文件成功");
    }

    /**
     * 验证文件md5 判断是否已经上传
     *
     * @param fileUploadDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "checkmd5", method = RequestMethod.POST)
    @CatchErr(value = "验证文件md5失败")
    public ResultMsg checkmd5(@RequestBody FileUploadDTO fileUploadDTO) throws Exception {
        if (fileUploadDTO == null || StrUtil.isBlank(fileUploadDTO.getMd5())) {
            return getWarnResult("验证失败，文件md5为空");
        }
        EtechFileTemp temp = etechWebUploaderService.checkmd5(fileUploadDTO);
        return getSuccessResult(temp, "验证文件md5成功");
    }
}
